package com.baixun.gascommon.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成token工具和校验token工具
 * */
@Component
public class TokenUntil {


    public static String secret="ShangHaiGas2024";


    // 生成token
    public  String createToken(String userInfo, List<String> collect) {
        //创建请求头，放入协议和算法名称
        Map<String, Object> headerClaims=new HashMap();
        headerClaims.put("type","JWT");
        headerClaims.put("alg","HS256");
        //创建签发时间，过期时间默认为2个小时
        Date currentDate = new Date();
        //有效期同步redis有效期
        Date expDate = new Date(currentDate.getTime() + (1000 * 60 * 60 * 24*5));
        return JWT.create()
                .withHeader(headerClaims)
                //发布日期
                .withIssuedAt(currentDate)
                //有效期
                .withExpiresAt(expDate)
                //自定义声明
                .withIssuer("ShangHaiGus")
                .withClaim("userInfo",userInfo)
                .withClaim("authList",collect)
                .sign(Algorithm.HMAC256(secret));


    }


    //校验token
    public  boolean checkToken(String token){
        //创建token校验器
        JWTVerifier build = JWT.require(Algorithm.HMAC256(secret)).build();
        try {
            //验证失败会自动抛出异常
            DecodedJWT verify = build.verify(token);
            return true;
        }catch (Exception e){
            return false;
        }
    }
//eyJ0eXAiOiJKV1QiLCJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1c2VySW5mbyI6IntcImlkXCI6MSxcInVzZXJOYW1lXCI6XCJhZG1pblwiLFwicGFzc1dvcmRcIjpcImRmMTBlZjg1MDlkYzE3NmQ3MzNkNTk1NDllN2RiZmFmXCIsXCJwaG9uZVwiOm51bGwsXCJzYWx0XCI6XCJhYmNcIixcImNyZWF0ZVRpbWVcIjpcIjIwMjQtMDktMjZUMTg6MDE6MjMuMDAwKzAwOjAwXCIsXCJzdGF0dXNcIjowLFwiZGVsZXRlZFwiOjB9IiwiaXNzIjoiU2hhbmdIYWlHdXMiLCJleHAiOjE3Mjg3Mjk2OTAsImF1dGhMaXN0IjpbInN5czp1cGRhdGUiLCJzeXM6dXBkYXRlIiwic3lzOnVwZGF0ZSIsInN5czp1cGRhdGUiLCJzeXM6dXBkYXRlIl0sImlhdCI6MTcyODcxMTY5MH0.rVL-r-iG2k7pFLfeKauIGpUKKbDjEwiaGEMO9UWNKAo


    //获取用户信息
    public String getUserInfo(String token){
        //创建token校验器
        JWTVerifier build = JWT.require(Algorithm.HMAC256(secret)).build();
        try {
            //验证失败会自动抛出异常
            DecodedJWT verify = build.verify(token);
            return verify.getClaim("userInfo").asString();
        }catch (Exception e){
            return null;
        }

    }


    //获取用户信息
    public List<String> getAutho(String token){
        //创建token校验器
        JWTVerifier build = JWT.require(Algorithm.HMAC256(secret)).build();
        try {
            //验证失败会自动抛出异常
            DecodedJWT verify = build.verify(token);
            return verify.getClaim("authList").asList(String.class);
        }catch (Exception e){
            return null;
        }

    }



}
